﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using Microsoft.SharePoint;
using System.Windows;

namespace SharePointCommander.Model.TreeView
{
    public class SCWeb : SCTreeViewItem<SPWeb>
    {
        public SCWeb(SCTreeViewItem parent, SPWeb web)
            : base(parent, web)
        {
            Header = web.Title; 
            Expanded += new RoutedEventHandler(SCWeb_Expanded);
            ItemsSource = new object[] { null };
        }

        void SCWeb_Expanded(object sender, RoutedEventArgs e)
        {
            TreeViewItem item = e.Source as TreeViewItem;
            if (item.ItemsSource.Cast<object>().Contains(null))
            {
                item.ItemsSource = new TreeViewItem[] {
                    new SCListCollection(this, SPObject.Lists) as TreeViewItem,
                    new TreeViewItem() {
                        Header = "Webs",
                        ItemsSource = from SPWeb w in SPObject.Webs select new SCWeb(this, w)
                    },
                    new SCProperties(this, SPObject.AllProperties)
                    };
            }
        }
    }
}
